package com.ccloud.modules.motorfactory.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ccloud.api.motorfactory.domain.MMotorPart;
import com.ccloud.api.motorfactory.domain.MWarehouseRecord;
import com.ccloud.api.motorfactory.service.IMWarehouseRecordService;
import com.ccloud.common.core.reqres.response.Result;
import com.ccloud.common.core.utils.OauthUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author chentl
 * @Version V1.0
 * @Project wh-bigdata-frame.
 * @Title WarehouseRecordController.java
 * @Description 出入库台账 控制器
 * @Package com.ccloud.api.motorfactory.controller
 * @Date 2022/4/9 17:52
 * @Copyright: 武汉大数据产业发展有限公司 All rights reserved.
 */
@Slf4j
@RestController
@RequestMapping("/warehouse-record")
public class WarehouseRecordController {

    @Resource
    private IMWarehouseRecordService warehouseRecordService;


    @GetMapping("/list")
    public Result<IPage<MWarehouseRecord>> list(MWarehouseRecord warehouseRecord,
                                          @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Result<IPage<MWarehouseRecord>> result = Result.ok();
        Page<MWarehouseRecord> page = new Page<>(pageNo, pageSize);

        IPage<MWarehouseRecord> pageList = warehouseRecordService.selectPage(page, warehouseRecord);

        result.setResult(pageList);
        return result;
    }

    @PostMapping("/add")
    public Result<?> add(@RequestBody MWarehouseRecord warehouseRecord) {
        Result<MMotorPart> result = new Result<>();

        warehouseRecordService.saveAndSyncWarehouseSum(warehouseRecord);

        return result;
    }

    @PutMapping("/update")
    public Result<MWarehouseRecord> update(@RequestBody MWarehouseRecord warehouseRecord) {
        Result<MWarehouseRecord> result = new Result<>();
        warehouseRecordService.updateById(warehouseRecord);
        return result;
    }

    @DeleteMapping("/delete")
    public Result<?> delete(@RequestParam("id") String id) {
        warehouseRecordService.removeById(id);
        return Result.ok();
    }


}
